『ロバストPython』10章 ピザ仕様クラスによる不変式の例
例:ピザの半径
intで型ヒント。floatが渡されているようなケースは型チェックで気付ける
assert 6 <= dough_radius_in_inches <= 12, 'Dough must be between 6 and 12 inches'
code:assert_behavior.py
>> dough_radius_in_inches = 7
>> assert 6 <= dough_radius_in_inches <= 12, 'Dough must be between 6 and 12 inches'
>> dough_radius_in_inches = 5
>> assert 6 <= dough_radius_in_inches <= 12, 'Dough must be between 6 and 12 inches'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: Dough must be between 6 and 12 inches
例:ピザのトッピング
リストで表す
先頭がソースになるように__init__で並び替える